package com.de.juxfux.io;

import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;


public class ProgressBarHandler extends Handler {
	private ProgressBar progressBar;
    private TextView progressTextView;
    
    public ProgressBarHandler(ProgressBar progressBar, TextView progressTextView) {
		super();
		this.progressBar = progressBar;
		this.progressTextView = progressTextView;
	}
    
    public void resetProgress() {
    	progressBar.setProgress(0);
    }

    public void setProgressMax(int max) {
    	progressBar.setMax(max);
    }

    public void addProgress(final int progress) {
    	post(new Runnable() {
    		public void run() {
    			progressBar.setProgress(progressBar.getProgress() + progress);
    		}
    	});
    }

    public void setProgress(final int newProgress) {
		post(new Runnable() {
			public void run() {
				progressBar.setProgress(newProgress);
			}
		});
    }
    
    public void setProgressText(final String newText) {
    	post(new Runnable() {
    		public void run() {
    			progressTextView.setText(newText);
    		}
    	});
    }
    
    public void showProgress() {
    	post(new Runnable() {
    		public void run() {
    			progressBar.setVisibility(View.VISIBLE);
    			progressTextView.setVisibility(View.VISIBLE);
    		}
    	});
    }
    
    public void hideProgress(int delay) {
    	try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
    	post(new Runnable() {
    		public void run() {
    	    	progressBar.setVisibility(View.GONE);
    	    	progressTextView.setVisibility(View.GONE);
    		}
    	});
    }
    
};

